package com.noriental.okcalls.wechat.kit;


import com.noriental.okcalls.wechat.config.ApiConfig;
import com.noriental.okcalls.wechat.config.WxApiConfigInstance;
import com.noriental.okcalls.wechat.kit.encrypt.AesException;
import com.noriental.okcalls.wechat.kit.encrypt.WXBizMsgCrypt;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;

/**
 * 对微信平台官方给出的加密解析代码进行再次封装
 * <p>
 * 异常java.security.InvalidKeyException:illegal Key Size的解决方案：
 * 1：在官方网站下载JCE无限制权限策略文件（JDK7的下载地址：
 * http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
 * 2：下载后解压，可以看到local_policy.jar和US_export_policy.jar以及readme.txt
 * 3：如果安装了JRE，将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件
 * 4：如果安装了JDK，将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件
 * <p>
 * <p>
 * 设置为消息加密模式后 JFinal Action Report 中有如下参数：
 * timestamp=1417610658
 * encrypt_type=aes
 * nonce=132155339
 * msg_signature=8ed2a14146c924153743162ab2c0d28eaf30a323
 * signature=1a3fad9a528869b1a73faf4c8054b7eeda2463d3
 */
public class MsgEncryptKit {

    private static final String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";

    /**
     * 加密公众号消息
     *
     * @param msg
     * @param timestamp
     * @param nonce
     * @return
     */
    public static String encrypt(String msg, String timestamp, String nonce) {
        try {
            ApiConfig ac = WxApiConfigInstance.apiConfig;
            return encryptMsg(msg, timestamp, nonce, ac.getAppId(), ac.getToken(), ac.getEncodingAesKey());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 解密公众号消息
     *
     * @param encryptedMsg
     * @param timestamp
     * @param nonce
     * @param msgSignature
     * @return
     */
    public static String decrypt(String encryptedMsg, String timestamp, String nonce, String msgSignature) {
        try {
            ApiConfig ac = WxApiConfigInstance.apiConfig;

            return decryptMsg(encryptedMsg, timestamp, nonce, msgSignature, ac.getAppId(), ac.getToken(), ac.getEncodingAesKey());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解密消息
     *
     * @param encryptedMsg
     * @param timestamp
     * @param nonce
     * @param msgSignature
     * @param appId
     * @param token
     * @param encodingAesKey
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     * @throws AesException
     */
    private static String decryptMsg(String encryptedMsg, String timestamp, String nonce, String msgSignature,
                                     String appId, String token, String encodingAesKey)
            throws ParserConfigurationException, SAXException, IOException, AesException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        StringReader sr = new StringReader(encryptedMsg);
        InputSource is = new InputSource(sr);
        Document document = db.parse(is);

        Element root = document.getDocumentElement();
        NodeList nodelist1 = root.getElementsByTagName("Encrypt");
        // NodeList nodelist2 = root.getElementsByTagName("MsgSignature");

        String encrypt = nodelist1.item(0).getTextContent();
        // String msgSignature = nodelist2.item(0).getTextContent();

        String fromXML = String.format(format, encrypt);

        if (encodingAesKey == null) {
            throw new IllegalStateException("encodingAesKey can not be null, config encodingAesKey first.");
        }

        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        return pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);    // 此处 timestamp 如果与加密前的不同则报签名不正确的异常
    }

    /**
     * 加密消息
     *
     * @param msg
     * @param timestamp
     * @param nonce
     * @param appId
     * @param token
     * @param encodingAesKey
     * @return
     * @throws AesException
     */
    private static String encryptMsg(String msg, String timestamp, String nonce, String appId, String token,
                                     String encodingAesKey) throws AesException {
        WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
        return pc.encryptMsg(msg, timestamp, nonce);
    }
}



